import { Controller } from "../../mvc/core/Controller";
import { VideoMessageHandler } from "../net/rmi/fight/VideoMessageHandler";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { SFightRecord } from "../../../Message/BroadCast/SFightRecord";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { Cache } from "../cache/Cache";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { SerializeStream } from "../../../Framework/Serialize/SerializeStream";
import { TimeMB } from "../net/rmi/fight/TimeMB";
import { FrameTimer } from "../../../com/gengine/core/frame/FrameTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
import { TimerAlertWin } from "../view/common/alertwins/TimerAlertWin";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { SceneConfig } from "../resource/SceneConfig";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { SPoint } from "../../../Message/Public/SPoint";
import { SPassTo } from "../../../Message/Public/SPassTo";
type int = number;
//class VideoController
    
    export  class VideoController extends Controller
    {
        constructor()
        {
            
            super();this._messagehandler = new VideoMessageHandler();
            return;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.FightRecord, this.onFightRecord);
            Dispatcher.addEventListener(EventName.VideoWatchVideo, this.onWatchVideo);
            Dispatcher.addEventListener(EventName.VideoExit, this.onVideoExit);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            return;
        }

        /* internal  */onWatchVideo(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            var loc2=int(loc1["copyType"]);
            var loc3=String(loc1["recordId"]);
            GameProxy.arena.watchFightRecord(loc2, loc3);
            return;
        }

        /* internal  */onVideoExit(arg1: DataEvent): void
        {
            Alert.show(Language.getString(44369), null, Alert.OK | Alert.CANCEL, null, this.onCloseExit);
            return;
        }

        /* internal  */onCloseExit(arg1: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.end();
            }
            return;
        }

        /* internal  */onFightRecord(arg1: SFightRecord): void
        {
            if (RolePlayer.instance.isInVideo) 
            {
                return;
            }
            this._fightRecord = arg1;
            GameProxy.copy.enterWatchRecordCopy();
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            var e: DataEvent;

            var loc1;
            e = arg1;
            if (this._fightRecord && !RolePlayer.instance.isInVideo && Cache.instance.copy.isInCopyType(ECopyType._ECopyWatchRecord)) 
            {
                with ({}) 
                {
                };
                
#error('pushwith') {}
                .flash.utils.setTimeout(
#error('pushwith') {}
                .view = (): void
                {
                    start(_fightRecord.mapId);
                    alyz(_fightRecord);
                    doCommand();
                    return;
                }, 200)
            }
            return;
        }

        /* internal  */alyz(arg1: SFightRecord): void
        {
            var loc2=0;
            var loc3=null;
            this._timeMBArray = [];
            var loc1=new SerializeStream();
            loc1.writeByteSeq(arg1.bytes);
            loc1.byteArray.position = 0;
            loc2 = loc1.readSize();
            loc2 = loc1.readSize();
            while (loc1.byteArray.position < loc1.byteArray.length) 
            {
                (loc3 = new TimeMB()).read(loc1);
                this._timeMBArray.push(loc3);
            }
            return;
        }

        /* internal  */doCommand(): void
        {
            if (!this._timer) 
            {
                this._timer = new FrameTimer();
            }
            this._timer.stop();
            this._timer.reset();
            this._timer.addListener(TimerType.ENTERFRAME, this.onTimer);
            this._timer.start();
            this.startTime = flash.utils.getTimer();
            return;
        }

        /* internal  */onTimer(arg1: FrameTimer): void
        {
            var loc2=null;
            var loc1=flash.utils.getTimer() - this.startTime;
            while (this._timeMBArray.length > 0) 
            {
                loc2 = this._timeMBArray[0];
                if (loc2.time < loc1) 
                {
                    this._messagehandler.onMessage(loc2.mb);
                    this._timeMBArray.shift();
                    continue;
                }
                break;
            }
            if (this._timeMBArray.length == 0) 
            {
                this._timer.stop();
                Alert.timerNO = true;
                Alert.mode = Alert.Mode_NoneNotModal;
                Alert.alertWinRenderer = TimerAlertWin;
                Alert.buttonWidth = 80;
                Alert.timerOut = 10;
                Alert.calcelLabel = Language.getString(44370);
                this._alertSp = Alert.show(Language.getString(44371), null, Alert.CANCEL, null, this.onClose);
            }
            return;
        }

        /* internal  */onClose(arg1: int): void
        {
            if (arg1 == Alert.CANCEL) 
            {
                this.end();
            }
            return;
        }

        public start(arg1: int): void
        {
            RolePlayer.instance.isInVideo = true;
            this._lastMapId = MapFileUtil.mapID;
            this._lastMapX = RolePlayer.instance.x;
            this._lastMapY = RolePlayer.instance.y;
            this._lastSpeed = RolePlayer.instance.entityInfo.entityInfo.speed;
            RolePlayer.instance.entityInfo.entityInfo.speed = 400;
            RolePlayer.instance.updateSpeed(400);
            var loc1=SceneConfig.instance.getSceneInfo(arg1).sMapDefine;
            this.changeRolePos(arg1, loc1.realWidth / 2, loc1.realHeight / 2);
            return;
        }

        public end(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
            }
            if (this._alertSp && this._alertSp.parent) 
            {
                this._alertSp.parent.removeChild(this._alertSp);
                this._alertSp = null;
            }
            RolePlayer.instance.entityInfo.entityInfo.speed = this._lastSpeed;
            RolePlayer.instance.updateSpeed(this._lastSpeed);
            RolePlayer.instance.isInVideo = false;
            GameProxy.copy.leftWatchRecordCopy();
            this._fightRecord = null;
            return;
        }

        /* internal  */changeRolePos(arg1: int, arg2: int, arg3: int): void
        {
            var loc1=GameMapUtil.getTilePoint(arg2, arg3);
            var loc2;
            (loc2 = new SPoint()).x = loc1.x;
            loc2.y = loc1.y;
            var loc3;
            (loc3 = new SPassTo()).mapId = arg1;
            loc3.passToId = 10;
            loc3.toPoint = loc2;
            Dispatcher.dispatchEvent(new DataEvent(ServerCommand.MapPointUpdate, loc3));
            return;
        }

        private /* var */_lastMapId: int;

        private /* var */_lastMapX: int;

        private /* var */_lastMapY: int;

        private /* var */_lastSpeed: int=180;

        private /* var */_timeMBArray: Array<any>;

        private /* var */_timer: FrameTimer;

        private /* var */_messagehandler: VideoMessageHandler;

        private /* var */_alertSp: Object /* flash.display.Sprite */;

        private /* var */_fightRecord: SFightRecord;

        private /* var */startTime: int;
    }
